package com.moorror.codegenvel.utils;

import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.ActionToolbar;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.IconLoader;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.util.ui.UIUtil;

import javax.swing.*;
import java.awt.*;

/**
 * @Author jiyanghuang
 * @Date 2022/8/7 13:50
 */
public class GuiUtils {
    private static final String ICON_FOLDER = "/icons/";

    public static final Icon SEQUENCE_OUTLINE = loadIcon("SequenceOutline.png");

    public static final Icon PLANT_UML = loadIcon("plantUml.png");

    public static final Icon DATE = loadIcon("date.png");

    public static final Icon RUN = loadIcon("run.svg");
    public static final Icon DEBUG = loadIcon("debug.svg");

    public static final Icon SCENE = loadIcon("scene.svg");
    public static final Icon MOCK = loadIcon("mock.svg");
    public static final Icon S_SQLITE = loadIcon("ssqlite.svg");
    public static final Icon D_SQLITE = loadIcon("dsqlite.svg");
    public static final Icon DELETE = loadIcon("delete.svg");
    public static final Icon SAVE = loadIcon("save.svg");
    public static final Icon ADD = loadIcon("add.svg");
    public static final Icon PUSH = loadIcon("push.svg");
    public static final Icon PREV = loadIcon("prev.svg");
    public static final Icon NEXT = loadIcon("next.svg");
    public static final Icon CURRENT = loadIcon("CURRENT.svg");
    public static final Icon M_REQ = loadIcon("mreq.svg");
    public static final Icon M_RESP = loadIcon("mresp.svg");
    public static final Icon SCAN = loadIcon("scan.svg");
    public static final Icon INVOKE = loadIcon("rtseq/InvokeAction.svg");
    public static final Icon SAVE_ICON = loadIcon("rtseq/SaveIcon.svg");
    public static final Icon EXTEND_METHOD = loadIcon("rtseq/ExtendMethod.svg");
    public static final Icon EXTEND_METHOD_NOTE = loadIcon("rtseq/ExtendMethodNote.svg");



    public static final Icon TEMP = loadIcon("temp.svg");
    public static final Icon RECORD_FOR_MOCK = loadIcon("RecordForMock.svg");
    public static final Icon RT_SEQUENCE = loadIcon("rtseq/RunTimeSequence.svg");
    public static final Icon RT_PROXY = loadIcon("rtseq/proxy.svg");
    public static final Icon RT_PROXY_NOTE = loadIcon("rtseq/ProxyNote.svg");
    public static final Icon RT_ASYNC = loadIcon("rtseq/async.svg");
    public static final Icon RT_ASYNC_NOTE = loadIcon("rtseq/AsyncNote.svg");
    public static final Icon LAMBDA_NOTE = loadIcon("rtseq/lambdaNote.svg");
    public static final Icon CLASS_NOTE = loadIcon("rtseq/classNote.svg");
    public static final Icon INTERFACE_NOTE = loadIcon("rtseq/interfaceNote.svg");
    public static final Icon ABSTRACT_CLASS_NOTE = loadIcon("rtseq/abstractClassNote.svg");
    public static final Icon ANONYMOUS_CLASS_NOTE = loadIcon("rtseq/anonymousClassNote.svg");
    public static final Icon RECURSIVE_METHOD_NOTE = loadIcon("rtseq/RecursiveMethodNote.svg");

    public static final Icon RT_ENTRANCE = loadIcon("rtseq/RuntimeEntrance.svg");
    public static final Icon RT_LOOP_FILTER_BY_SHOW_CONFIG = loadIcon("rtseq/LoopFilterByShowConfig.svg");
    public static final Icon RT_PUSH_AOP_CONFIG_DATA = loadIcon("rtseq/PushAopConfigData.svg");
    public static final Icon RT_SAVE_CURRENT_TREE_TO_SQLITE = loadIcon("rtseq/SaveCurrentTreeToSqlite.svg");
    public static final Icon RT_DELETE_TREE_FROM_SQLITE = loadIcon("rtseq/DeleteTreeFromSqlite.svg");

    public static final Icon IN_SEQ = loadIcon("inseq/InSequence.svg");
    public static final Icon IN_SEQ_ENTRANCE = loadIcon("inseq/InSequenceEntrance.svg");
    public static final Icon FG_SPLIT = loadIcon("fg/FGSplit.svg");

    public static final Icon DATA_EDITOR_BROWSE =    loadIcon("panel/ButtonBrowse.png");

    // codeGen
    public static final Icon CODE_GEN_VM = loadIcon("codegen/CodeGenVm.svg");


    public static Icon loadIcon(String iconFilename) {
        return IconLoader.findIcon(ICON_FOLDER + iconFilename, GuiUtils.class);
    }

    public static Icon loadIcon(String iconFilename, String darkIconFilename) {
        String iconPath = ICON_FOLDER;
        if (isUnderDarcula()) {
            iconPath += darkIconFilename;
        } else {
            iconPath += iconFilename;
        }
        return IconLoader.findIcon(iconPath, GuiUtils.class);
    }

    /**
     * 生成 ActionGroup 组
     * @param actionGroup
     * @param toolBarPanel
     * @param actionManager
     * @param toolbarName
     * @param horizontal
     */
    public static void installActionGroupInToolBar(DefaultActionGroup actionGroup, JPanel toolBarPanel, ActionManager actionManager, String place, boolean horizontal) {
        if (actionManager == null) {
            return;
        }

//        JComponent actionToolbar = ActionManager.getInstance().createActionToolbar(place, actionGroup, horizontal).getComponent();
//        toolBarPanel.add(actionToolbar, BorderLayout.CENTER);
        ActionToolbar actionToolbar = ActionManager.getInstance().createActionToolbar(place, actionGroup, horizontal);
        toolBarPanel.add(actionToolbar.getComponent(), BorderLayout.CENTER);
        actionToolbar.setTargetComponent(toolBarPanel);
//        toolBarPanel.add(actionToolbar, BorderLayout.CENTER);

    }

    public static Dimension enlargeWidth(Dimension preferredSize, double factor) {
        int enlargedWidth = Double.valueOf(preferredSize.width * factor).intValue();
        return new Dimension(enlargedWidth, preferredSize.height);
    }

    public static void showNotification(final JComponent component, final MessageType info, final String message, final Balloon.Position position) {
        UIUtil.invokeLaterIfNeeded(() -> JBPopupFactory.getInstance().createBalloonBuilder(new JLabel(message))
                .setFillColor(info.getPopupBackground())
                .createBalloon()
                .show(new RelativePoint(component, new Point(0, 0)), position));
    }

    private static boolean isUnderDarcula() {
        return UIManager.getLookAndFeel().getName().contains("Darcula");
    }
}
